package com.webshop.servlet;

import com.webshop.Dao.ProductDao;
import com.webshop.domain.Items;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Search")
public class Search extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String search = req.getParameter("search");
        List<Items> items = ProductDao.search(search);
        if (items.size()==0){
            resp.sendRedirect("nofound.jsp");
            return;
        }
        req.setAttribute("search","搜索");
        req.setAttribute("items",items);
        req.getRequestDispatcher("items.jsp").forward(req,resp);
        return;
    }
}
